<?php
/**
 * 
 * @author chenqi
 * @uses 订单
 * @date 2015-07-17
 *
 */
class Order extends WX_Controller{
   public function __construct()
	{
		parent::__construct();
		$this->load->model('appoint_model');
		$this->load->model('appoint_set_model');
	}
	
	//订单详情
	public function info()
	{	
		$appoint_id = $this->input->get('appoint_id');//订单号
		
		$order=$this->appoint_model->checkAppointByuser(array('appoint.id'=>$appoint_id));//获取订单信息及
		$order['areas'] = unserialize($order['areas']);
		$order['address'] = $order['areas']['detail'].$order['address'];
		$order['serverphone'] = $this->serverphone;
		$data['order'] = $order;
		//var_dump($order);
		
		if($order['master_user'] == $this->user['id']){//以接单小哥角色查看订单
			//echo "以接单小哥角色查看订单";
			$this->load->view('/wap/order_info.html',$data);//订单详情
		}else{//以普通用户（下单人）查看订单
			//echo "下单人查看订单";
			$this->load->view('/wap/order_info_yy.html',$data);//订单详情
		}
	}
	
	//我的订单（已完成、已预约、已取消）
	public function my_order(){
		$this->load->model('appoint_model');
		$data = $_GET;
		$master_id = $data['id'];
		$status = $data['status'];
		$where['master_id'] = $master_id;
		$where['appoint_status'] = 1;
		$this->data['unfinished'] = $this->appoint_model->Appoint($where);
		$where['appoint_status'] = 2;
		$this->data['cancel'] = $this->appoint_model->Appoint($where);
		$where['appoint_status'] = 3;
		$this->data['success'] = $this->appoint_model->Appoint($where);
		$this->data['status'] = $status;
		$this->load->view('/wap/order_list.html',$this->data);
	}
	//我的取消订单
	public function my_cancel(){
		$this->load->model('appoint_model');
		$where['appoint_status'] = 4;
		$where['change_object'] = 2;
		$this->data['data'] = $this->appoint_model->Appoint($where);
		$this->load->view('/wap/order_cancel.html',$this->data);
	}
	
	//历史订单（普通用户）
	public function history(){
		$userid = $this->user['id'];	
		$where['user_id'] = $userid;
		$list = $this->appoint_model->getAppointPage($where);
		foreach($list as $k=>&$v){
			$v['areas']=unserialize($v['areas']);
			$v['address'] =$v['areas']['detail'].$v['address'];
		}
		$data['list'] = $list;
		//var_dump($list);
		$this->load->view('/wap/history.html',$data);
	}
	
	//用户反馈
	public function feedback(){
		$feedback_msg = $this->input->get('feedback_msg');
		$data = array("user_id"=>$this->user['id'],'content'=>$feedback_msg,"create_time"=>time());
		$this->load->model('comment_model');
		$res=$this->comment_model->addComment($data);
		if($res){
			$this->response(array('status'=>true, 'data'=>'感谢您的建议'));
		}
		$this->response(array('status'=>false, 'data'=>'提交建议出错'));
	}
	//用户评价
	public function comment(){
		$comment_msg = $this->input->get('comment_msg');
		$order_id = $this->input->get('order_id');
		$res = $this->appoint_model->editAppoint(array('id'=>$order_id),array("comment"=>$comment_msg));
		if($res){
			$this->response(array('status'=>true, 'data'=>'感谢您的建议'));
		}
		$this->response(array('status'=>false, 'data'=>'提交建议出错'));
		
	} 
	
	//申请退单page
	public function back(){
		$type = $this->input->get('type');//1 取消 2更换小哥
		$obj = $this->input->get('obj');//操作对象
		$order_id = $this->input->get('order_id');
		$data['type'] = $type;
		
		$data['order_id'] = $order_id;
		$data['obj'] = $obj;
		if($obj == 1){
			if($type == 1){
			$this->load->view('/wap/back.html',$data);//申请退单界面(用户)
			}else{
				$this->load->view('/wap/back_change.html',$data);//更换小哥界面(用户)
			}
		}else{
			$this->load->view('/wap/back_xg.html',$data);//(小哥进行更换小哥)
		}
	}
	
	//检查订单是否满足消息条件
	public function check_cancel(){
		$order_id = $this->input->get('order_id');
		//不得超过预约时间40分钟
		$orderinfo=$this->appoint_model->checkAppoint(array('appoint.id'=>$order_id));
		
		$limit_set = $this->appoint_set_model->checkSet(array('id'=>1));
		
		if($this->current_time+$limit_set['cancel_time']*60 > $orderinfo['appoint_time']){
			$this->response(array('status'=>false, 'data'=>'临近预约时间'.$limit_set['cancel_time'].'分钟不能取消订单'));
		}
		$this->response(array('status'=>true));
	}
	//检查订单是否满足更换条件
	public function check_change(){
		$order_id = $this->input->get('order_id');
		$obj = $this->input->get('obj');
		//不得超过预约时间40分钟
		$orderinfo=$this->appoint_model->checkAppoint(array('appoint.id'=>$order_id));
		
		$limit_set = $this->appoint_set_model->checkSet(array('id'=>1));
		if($this->current_time+$limit_set['turn_time']*60 > $orderinfo['appoint_time']){
			if($obj == 2){
				$this->response(array('status'=>false, 'data'=>'临近预约时间'.$limit_set['cancel_time'].'分钟不能转单','time'=>$this->current_time+$limit_set['turn_time']*60,'etime'=>$orderinfo['appoint_time'],'order'=>$orderinfo));
			}else{
				$this->response(array('status'=>false, 'data'=>'临近预约时间'.$limit_set['cancel_time'].'分钟不能更换小哥'));
			}
		}
				
		if($obj == 2){//对象是小哥
			$this->load->model('master_model');
			$master=$this->master_model->checkMaster(array("user_id"=>$this->user['id']));

			if($master['turn_num'] >= $limit_set['appoint_limit']){
				$this->response(array('status'=>false, 'data'=>'超过'.$limit_set['appoint_limit'].'次的转单限制次数，不能转单'));
			}
		}
		$this->response(array('status'=>true));
		
	}
	
	//取消订单(退单)
	//@todo 当有小哥接单了的时候，取消是否通知小哥
	public function deal_cancel(){
		$order_id = $this->input->get('order_id');
		$cause = $this->input->get('cause');		
		
		$data['change_reason'] = $cause;
		$data['appoint_status'] = 2; //订单改为取消状态
		
		$res = $this->appoint_model->editAppoint(array('id'=>$order_id),$data);
		if($res){
			$this->response(array('status'=>true, 'data'=>'取消订单成功'));
		}
		$this->response(array('status'=>false, 'data'=>'取消订单失败'));
	}
	
	//更换小哥
	public function deal_change(){
		$order_id = $this->input->get('order_id');
		$cause = $this->input->get('cause');
		$obj = $this->input->get('obj');
		
		$data['change_reason'] = $cause;//转单原因
		$data['change_status'] = 2; //转单待审核
		$data['change_object'] = $obj;//转单操作对象
		
		$res = $this->appoint_model->editAppoint(array('id'=>$order_id),$data);
		if($res){
			$this->response(array('status'=>true, 'data'=>'更换操作成功,等待审核'));
		}
		$this->response(array('status'=>false, 'data'=>'更换小哥失败'));
	}
	//完成订单
	public function finish(){
		$order_id = $this->input->get('order_id');
		$data['appoint_status'] = 3;//完成状态
		$data['update_time'] = $this->current_time;
		$res = $this->appoint_model->editAppoint(array('id'=>$order_id),$data);
		if($res){
			$this->response(array('status'=>true, 'data'=>'完成订单成功'));
		}
		$this->response(array('status'=>false, 'data'=>'操作失败'));
	}
}